(require 'gtags)
(setq gtags-mode-map '(keymap)) ;;;; 取消gtags默认的键定义，必须放在开启模式之前
(autoload 'gtags-mode "gtags"
  "Minor mode for browsing source code using GLOBAL" t)
(add-hook 'gtags-mode-hook
      #'(lambda ()
         (setq gtags-path-style 'relative)
         (setq gtags-auto-update t)))

(define-prefix-command 'starsoul-gtags-map)
(global-set-key (kbd "C-x C-g") 'starsoul-gtags-map)
;; 从光标处取得tag并查找
(define-key starsoul-gtags-map (kbd "C-t") 'gtags-find-tag-from-here)
;; 从鼠标处取得gtag并查找
(define-key starsoul-gtags-map (kbd "C-<mouse-1>") 'gtags-find-tag-by-event)
;; Enable template insertion menu
(define-key starsoul-gtags-map (kbd "C-v") 'gtags-visit-rootdir)
;; find the defination of functions
(define-key starsoul-gtags-map (kbd "C-a") 'gtags-find-tag)
;; find referances of functions
(define-key starsoul-gtags-map (kbd "C-r") 'gtags-find-rtag)
;; find symbols
(define-key starsoul-gtags-map (kbd "C-s") 'gtags-find-symbol)
;; find strings
(define-key starsoul-gtags-map (kbd "C-p") 'gtags-find-pattern)
;; find strings
(define-key starsoul-gtags-map (kbd "C-g") 'gtags-find-with-grep)
;; find strings
(define-key starsoul-gtags-map (kbd "C-i") 'gtags-find-with-idutils)
;; find file
(define-key starsoul-gtags-map (kbd "C-f") 'gtags-find-file)
;; analysis file
(define-key starsoul-gtags-map (kbd "C-p") 'gtags-parse-file)
;; display brower
(define-key starsoul-gtags-map (kbd "C-d") 'gtags-display-browser)
;; return
(define-key starsoul-gtags-map (kbd "C-b") 'gtags-pop-stack)
(define-key starsoul-gtags-map (kbd "C-<mouse-3>") 'gtags-pop-stack)

;; update gtags
(define-key starsoul-gtags-map (kbd "C-u") 'gtags-update-prj)

(defun gtags-update-prj ()
  (interactive)
  (if gtags-mode
    (progn
      (message "start to global -u")
      (gtags-push-tramp-environment)
      (call-process gtags-global-command nil nil nil "-u" (concat "--single-update=" (gtags-buffer-file-name)))
      (gtags-pop-tramp-environment)
      (message "finish to global -u"))))

(provide 'init_gtags)
